using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections.ObjectModel;

namespace planning
{
	public class Enseignant : Ressource
	{
		// ----- ATTRIBUTS -----
		private string prenom;
		private string nom;

		// ----- CONSTRUCTEURS -----
		public Enseignant()
		{
			// Start of user code for default constructor
			// End of user code for default constructor
		}

		// ----- ACCESSEURS -----
		public string Prenom
		{
			get { return prenom; }
			set { prenom = value; ObjectChanged("prenom"); }
		}

		public string Nom
		{
			get { return nom; }
			set { nom = value; ObjectChanged("nom"); }
		}


		private static Dictionary<string, Enseignant> references = new Dictionary<string, Enseignant>();

		public static new Enseignant fromXml(XmlNode node)
		{
			Enseignant enseignant;
			XmlNode refAttribute = node.SelectSingleNode("@ref");
			XmlNode idAttribute = node.SelectSingleNode("@id");

			if(refAttribute != null) {
				if(references.ContainsKey(refAttribute.Value)) {
					return references[refAttribute.Value];
				} else {
					enseignant = new Enseignant();
					references.Add(refAttribute.Value, enseignant);
					return enseignant;
				}
			}

			if(idAttribute != null && references.ContainsKey(idAttribute.Value)) {
				enseignant = references[idAttribute.Value];
			} else {
				enseignant = new Enseignant();

				if(idAttribute != null) {
					references.Add(idAttribute.Value, enseignant);
				}
			}

			if(node.SelectSingleNode("@prenom") != null)
				enseignant.Prenom = node.SelectSingleNode("@prenom").Value;

			if(node.SelectSingleNode("@nom") != null)
				enseignant.Nom = node.SelectSingleNode("@nom").Value;

			

			return enseignant;

		}

        public string Ref
        {
            get
            {
                return "ens-" + Prenom.ToLower()[0] + Nom.ToLower();
            }
        }

        public override XmlElement toXml(XmlDocument document)
		{
			// Start of user code for toXml
            XmlElement element = document.CreateElement("Enseignant");
            XmlAttribute refAttribute = document.CreateAttribute("id");
            refAttribute.Value = Ref;
            element.Attributes.Append(refAttribute);

            XmlAttribute prenomAttribute = document.CreateAttribute("prenom");
            prenomAttribute.Value = Prenom;
            element.Attributes.Append(prenomAttribute);
            
            XmlAttribute nomAttribute = document.CreateAttribute("nom");
            nomAttribute.Value = Nom;
            element.Attributes.Append(nomAttribute);

            return element;
			// End of user code for toXml
		}


	}

}
